package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.DualListModel;

import primetest.dto.DepartmentDto;
import primetest.dto.DoctorDto;
import primetest.dto.PatientDto;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.DepartmentService;
import primetest.impl.service.DoctorService;

@Named("registerControler")
@SessionScoped
public class RegisterControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -8695100248163797598L;

	private DualListModel<DoctorDto> doctors = new DualListModel<DoctorDto>(
			new ArrayList<DoctorDto>(), new ArrayList<DoctorDto>());
	public DualListModel<DoctorDto> getDoctors() {
		return doctors;
	}

	public void setDoctors(DualListModel<DoctorDto> doctors) {
		this.doctors = doctors;
	}

	private List<DoctorDto> onlineDoctors = new ArrayList<DoctorDto>();
	private List<DoctorDto> offlineDoctors = new ArrayList<DoctorDto>();

	private List<DoctorDto> onlineDoctorsForPatient = new ArrayList<DoctorDto>();


	@Inject
	private DepartmentService departmentService;
	@Inject
	private DoctorService doctorService;


	@Inject
	private SessionManager sessionManager;

	private DualListModel<DepartmentDto> departments;
	private List<DepartmentDto> onlineDepartments;
	private List<DepartmentDto> offlineDepartments;

	private int selectedDepartment;

	private int selectedDepartmentForPatient;
	private int selectedDoctorForPatient;
	private PatientDto selectedPatient;
	private boolean patientRegisterMode = false;

	public int getSelectedDoctorForPatient() {
		return selectedDoctorForPatient;
	}

	public void setSelectedDoctorForPatient(int selectedDoctorForPatient) {
		this.selectedDoctorForPatient = selectedDoctorForPatient;
	}

	public int getSelectedDepartment() {
		return selectedDepartment;
	}

	public void setSelectedDepartment(int selectedDepartment) {
		this.selectedDepartment = selectedDepartment;
	}

	public void onSelectDepartment() throws IOException {
		if (selectedDepartment != -1) {
			List<DoctorDto> all = doctorService.getDoctorListByDepartment(selectedDepartment);
			onlineDoctors.clear();
			offlineDoctors.clear();
			for (DoctorDto d : all) {
				if (d.isOnline()) {
					onlineDoctors.add(d);
				} else {
					offlineDoctors.add(d);
				}
			}
			doctors = new DualListModel<DoctorDto>(offlineDoctors, onlineDoctors);
		}
	}

	public void onSelectDepartmentForPatient() throws IOException {
		if (selectedDepartmentForPatient != -1) {
			List<DoctorDto> all = doctorService.getDoctorListByDepartment(selectedDepartmentForPatient);
			onlineDoctorsForPatient.clear();
			for (DoctorDto d : all) {
				if (d.isOnline()) {
					onlineDoctorsForPatient.add(d);
				}
			}
		}
	}

	public void init() throws IOException {
		onlineDepartments = new ArrayList<DepartmentDto>();
		offlineDepartments = new ArrayList<DepartmentDto>();

		List<DepartmentDto> all = departmentService.getDepartmentList();
		for (DepartmentDto d : all) {
			if (d.isOnline()) {
				onlineDepartments.add(d);
			} else {
				offlineDepartments.add(d);
			}
		}
		departments = new DualListModel<DepartmentDto>(offlineDepartments,
				onlineDepartments);
	}

	public DualListModel<DepartmentDto> getDepartments() throws IOException {
		if (departments == null) {
			init();
		}
		return departments;
	}

	public void setDepartments(DualListModel<DepartmentDto> departments) {
		this.departments = departments;
	}

	public void submit() {
		offlineDepartments = departments.getSource();
		onlineDepartments = departments.getTarget();
		for (DepartmentDto d : offlineDepartments) {
			d.setOnline(false);
		}
		for (DepartmentDto d : onlineDepartments) {
			d.setOnline(true);
		}
		sessionManager.addGlobalMessageInfo("Department status changed", null);
	}

	public void submit2() throws IOException {
		if (selectedDepartment != -1) {
			offlineDoctors = doctors.getSource();
			onlineDoctors = doctors.getTarget();

			for (DoctorDto d : offlineDoctors) {
				d.setOnline(false);
			}
			for (DoctorDto d : onlineDoctors) {
				d.setOnline(true);
			}
			sessionManager.addGlobalMessageInfo("Doctor status changed", null);
		}
	}

	public void startRegister() {
		patientRegisterMode = true;
	}

	public void register() throws IOException {
		if (selectedDepartmentForPatient == -1) {
			sessionManager.addGlobalMessageFatal("Please specify department", null);
			return;
		}
		if (selectedDoctorForPatient == -1) {
			sessionManager.addGlobalMessageFatal("Please specify doctor", null);
			return;
		}
		DoctorDto doctor = doctorService.findDoctorBySeq(selectedDoctorForPatient);
		doctor.getPatientQueue().add(selectedPatient);
		doctor.getPatientList().add(selectedPatient);
		patientRegisterMode = false;
		sessionManager.addGlobalMessageInfo("Patient register successful", null);
	}

	public void cancelRegister() {
		patientRegisterMode = false;
	}

	public List<DepartmentDto> getOnlineDepartments() throws IOException {
		if (onlineDepartments == null) {
			init();
		}
		return onlineDepartments;
	}

	public void setOnlineDepartments(List<DepartmentDto> onlineDepartments) {
		this.onlineDepartments = onlineDepartments;
	}

	public List<DepartmentDto> getOfflineDepartments() {
		return offlineDepartments;
	}

	public void setOfflineDepartments(List<DepartmentDto> offlineDepartments) {
		this.offlineDepartments = offlineDepartments;
	}

	public List<DoctorDto> getOfflineDoctors() {
		return offlineDoctors;
	}

	public void setOfflineDoctors(List<DoctorDto> offlineDoctors) {
		this.offlineDoctors = offlineDoctors;
	}

	public List<DoctorDto> getOnlineDoctors() {
		return onlineDoctors;
	}

	public void setOnlineDoctors(List<DoctorDto> onlineDoctors) {
		this.onlineDoctors = onlineDoctors;
	}

	public int getSelectedDepartmentForPatient() {
		return selectedDepartmentForPatient;
	}

	public void setSelectedDepartmentForPatient(int selectedDepartmentForPatient) {
		this.selectedDepartmentForPatient = selectedDepartmentForPatient;
	}

	public List<DoctorDto> getOnlineDoctorsForPatient() {
		return onlineDoctorsForPatient;
	}

	public void setOnlineDoctorsForPatient(List<DoctorDto> onlineDoctorsForPatient) {
		this.onlineDoctorsForPatient = onlineDoctorsForPatient;
	}

	public PatientDto getSelectedPatient() {
		return selectedPatient;
	}

	public void setSelectedPatient(PatientDto selectedPatient) {
		this.selectedPatient = selectedPatient;
	}

	public boolean isPatientRegisterMode() {
		return patientRegisterMode;
	}

	public void setPatientRegisterMode(boolean patientRegisterMode) {
		this.patientRegisterMode = patientRegisterMode;
	}
}
